package zebbe.android.opengl;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLU;

import zebbe.android.opengl.support.GameActivity;

/**
 * @author dzsebedi
 * 
 */
public class Renderer {

	private float vertices[] = { 0.0f, 0.5f, 0.0f, // 0, Top
			-0.5f, -0.5f, 0.0f, // 1, Bottom Left
			0.5f, -0.5f, 0.0f, // 2, Bottom Right
	// 1.0f, 1.0f, 0.0f, // 3, Top Right
	};

	private FloatBuffer vertexBuffer;

	public Renderer(GL10 gl, GameActivity activity) {

		// a float is 4 bytes, therefore we multiply the number if vertices with
		// 4.
		ByteBuffer buffer = ByteBuffer.allocateDirect(vertices.length * 4);
		buffer.order(ByteOrder.nativeOrder());
		vertexBuffer = buffer.asFloatBuffer();
		vertexBuffer.put(vertices);
		vertexBuffer.rewind();
	}

	public void render(GL10 gl, GameActivity activity, Simulation simulation) {
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		gl.glViewport(0, 0, activity.getViewportWidth(),
				activity.getViewportHeight());
		setProjectionAndCamera(gl, activity);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
		gl.glTranslatef(0, 0, -4);
		gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
		
		simulation.cube.draw(gl);
//		cube.draw(gl);
	}
	
	private void setProjectionAndCamera( GL10 gl, GameActivity activity )
    {
            gl.glMatrixMode( GL10.GL_PROJECTION );
            gl.glLoadIdentity();
            float aspectRatio = (float)activity.getViewportWidth() / activity.getViewportHeight();
            GLU.gluPerspective( gl, 67f, aspectRatio, 1f, 1000f );                                    
           
            gl.glMatrixMode( GL10.GL_MODELVIEW );
            gl.glLoadIdentity();
//            GLU.gluLookAt(gl, 0, 0, 4, 0, 0, -1, 0, 1, 0);
//            GLU.gluLookAt( gl, ship.position.x, 6, 2, ship.position.x, 0, -4, 0, 1, 0 );
    }


	public void dispose() {
		// TODO Auto-generated method stub

	}

}
